//
//  AppDelegate+RootController.m
//  basic
//
//  Created by Jack on 2016/12/16.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "AppDelegate+RootController.h"
#import "UWOADViewController.h"

@interface AppDelegate ()<UIScrollViewDelegate>

@end

@implementation AppDelegate (RootController)

+ (AppDelegate *)instance {
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

#pragma mark - 引导页
- (void)leadingScrollView
{//引导页
    UIScrollView *sc = [[UIScrollView alloc]initWithFrame:self.window.bounds];
    sc.pagingEnabled = YES;
    sc.delegate = self;
    sc.showsHorizontalScrollIndicator = NO;
    sc.showsVerticalScrollIndicator = NO;
    [self.window.rootViewController.view addSubview:sc];
    
    NSArray *arr = @[@"leading_1.jpg",@"leading_2.jpg",@"leading_3.jpg",@"leading_4.jpg"];
    for (NSInteger i = 0; i<arr.count; i++)
    {
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW*i, 0, kScreenW, self.window.frame.size.height)];
        img.image = [UIImage imageNamed:arr[i]];
        [sc addSubview:img];
        img.userInteractionEnabled = YES;
        if (i == arr.count - 1)
        {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn.frame = CGRectMake((self.window.frame.size.width/2)-50, kScreenH-110, 100, 40);
            btn.backgroundColor = [UIColor blueColor];
            [btn setTitle:@"开始体验" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(goToMain) forControlEvents:UIControlEventTouchUpInside];
            [img addSubview:btn];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.layer.borderWidth = 1;
            btn.layer.borderColor = UWOWhite.CGColor;
        }
    }
    sc.contentSize = CGSizeMake(kScreenW*arr.count, self.window.frame.size.height);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    DLog(@"scrollView.contentOffset.x == %f",scrollView.contentOffset.x);
    if (scrollView.contentOffset.x>kScreenW *3+40)
    {
        [self goToMain];
    }
}

- (void)goToMain
{
    [self setRoot:[UWOTabBarController new]];
}

#pragma mark - Windows

- (void)setAppWindows
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [[UIApplication sharedApplication]setStatusBarHidden:NO];
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}

- (void)setRootViewController:(UIViewController *)rootVC
{
    if(rootVC != nil){
        [self setRoot:rootVC];
        return;
    }
    
    NSString *key = @"CFBundleShortVersionString";
    // 上一次的使用版本（存储在沙盒中的版本号）
    NSString *lastVersion = [UWOUserDefault objectForKey:key];
    // 当前软件的版本号（从Info.plist中获得）
    NSString *currentVersion = APP_VERSION;
    if ([currentVersion isEqualToString:lastVersion])
    {
        // TODO 跳转广告页面
        UWOADViewController *adVc = [[UWOADViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:adVc];
        
        [self setRoot:nav];
        
    }
    else
    {
        //不是第一次安装或者是新版本
        // 将当前的版本号存进沙盒
        [UWOUserDefault setObject:currentVersion forKey:key];
        [UWOUserDefault synchronize];
        
        //引导页面、新特新
        UIViewController *emptyView = [[ UIViewController alloc ]init ];
        self.window.rootViewController = emptyView;
        [self leadingScrollView];
        
    
    }
}

- (void)setRoot:(UIViewController *)rootVC
{
    self.window.rootViewController = rootVC;
}

@end
